package leaderus.study;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

public class PersonLimitMNByQuickSort {
	public static void main(String[] args) {
		String tableFile=GenRandomersonDBFile.path + "//"+ GenRandomersonDBFile.fileName;
		PersonLimitMNByQuickSort personLimitMNByQuickSort = new PersonLimitMNByQuickSort();
		int[] intArray=personLimitMNByQuickSort.loadSortArray(tableFile);
		long startTime=System.currentTimeMillis();
		personLimitMNByQuickSort.quickSort(intArray, 0, 1000000-1);
		System.out.println("快速排序耗时："+(System.currentTimeMillis()-startTime)+"ms");
		/*for(int i=900000;i<900100;i++){
			System.out.println(intArray[i]);
		}*/
	}
	void quickSort(int s[], int l, int r)  
	{  
	    if (l < r)  
	    {  
	        int i = l, j = r, x = s[l];  
	        while (i < j)  
	        {  
	            while(i < j && s[j] >= x) // 从右向左找第一个小于x的数  
	                j--;    
	            if(i < j)   
	                s[i++] = s[j];  
	              
	            while(i < j && s[i] < x) // 从左向右找第一个大于等于x的数  
	                i++;    
	            if(i < j)   
	                s[j--] = s[i];  
	        }  
	        s[i] = x;  
	        quickSort(s, l, i - 1); // 递归调用   
	        quickSort(s, i + 1, r);  
	    }  
	}
	
	public int[] loadSortArray(String fileName){
		File personFile=new File(fileName);
		int[] sortArray=new int[1000000];
		if (personFile.exists() && personFile.isFile()) {
			try {
				InputStreamReader read = new InputStreamReader(new FileInputStream(personFile));
				BufferedReader bufferedReader = new BufferedReader(read);
				String infoLine;
				String[] infos;
				int i=0;
				while ((infoLine = bufferedReader.readLine()) != null) {
					infos = infoLine.split(",");
					sortArray[i]=Integer.valueOf(infos[0]);
					i++;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else{
			System.out.println("找不到指定文件！");
		}
		return sortArray;
	}
}
